/*******************************************************
 * Title: ConfigFactory.java 
 * Description: 
 * 
 * Copyright 2009. silverduner.com. 
 * All rights reserved.
 *******************************************************/
package com.silverduner.config;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationUtils;

/**
 * @author Yuwen Sun
 * @version 
 */
public class ConfigFactory {
	private static Map<URL,Config> _configMap = new HashMap<URL,Config>();
	private static final String DEFAULT_CONFIG_FILE = "config.xml";
	
	public static Config getConfig() throws ConfigException{
		return getConfig(ClassLoader.getSystemResource(DEFAULT_CONFIG_FILE));
	}
	
	public static Config getConfig(String filename) throws ConfigException{
		return getConfig(ConfigurationUtils.locate(filename));
	}
	
	public static Config getConfig(URL url) throws ConfigException{
		synchronized (_configMap) {
			if(!_configMap.containsKey(url))
				_configMap.put(url,new DefaultConfigImpl(url));
		}
		return _configMap.get(url);
	}
	
	public static Config getConfig(File file) throws ConfigException, MalformedURLException{
		return getConfig(ConfigurationUtils.getURL(null, file.getAbsolutePath()));
	}
}
